package com.mdz.sbgl.Controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Entity.*;
import com.mdz.sbgl.Mapper.SbflbMapper;
import com.mdz.sbgl.Mapper.WhflbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/sbgl/whflb")
public class WhflbController {

    @Autowired
    private WhflbMapper whflbMapper;

    @Autowired
    private HttpSession session;

    @ResponseBody
    @RequestMapping(value = "/getgzyy/{gsid}",method = RequestMethod.GET)
    public List<Whflb> getgzyy(ModelMap map,@PathVariable("gsid") String gsid){
        List<Whflb> whflbs=whflbMapper.getgzyy(gsid);
        map.put("whflbs",whflbs);
        return whflbs;
    }


    @ResponseBody
    @RequestMapping(value = "/getgzyyforid/{gsid}/{gzfl}",method = RequestMethod.GET)
    public List<Whflb> getgzyyforid(ModelMap map,@PathVariable("gsid") String gsid,@PathVariable("gzfl") String gzfl){
        List<Whflb> whflbs=whflbMapper.getgzyyforid(gsid,gzfl);
        map.put("whflbs",whflbs);
        return whflbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getgzfl/{gsid}",method = RequestMethod.GET)
    public List<Whflb> getgzfl(ModelMap map,@PathVariable("gsid") String gsid){
        List<Whflb> whflbs=whflbMapper.getgzfl(gsid);
        map.put("whflbs",whflbs);
        return whflbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getjjfa/{gzyy}/{gsid}",method = RequestMethod.GET)
    public List<Whflb> getnamebyans(@PathVariable("gzyy") String gzyy,@PathVariable("gsid") String gsid){
        List<Whflb> whflbs=whflbMapper.getjjfa(gzyy,gsid);
        return whflbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getwhflb/{gsid}",method = RequestMethod.GET)
    public List<Whflb> getwhflb(@PathVariable("gsid") String gsid){
        List<Whflb> whflbs=whflbMapper.selectList(new QueryWrapper<Whflb>().eq("gsid",gsid));
        return whflbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getans/{gzyy}",method = RequestMethod.GET)
    public List<Whflb> getans(@PathVariable("gzyy") String gzyy){
        List<Whflb> whflbs=whflbMapper.selectList(new QueryWrapper<Whflb>().eq("gzyy",gzyy));
        return whflbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getgzyybyfl/{gsid}/{gzfl}",method = RequestMethod.GET)
    public List<Whflb> getgzyybyfl(@PathVariable("gsid") String gsid,@PathVariable("gzfl") String gzfl){
        List<Whflb> whflbs=whflbMapper.getgzyybyfl(gsid,gzfl);
        List list=new ArrayList<Whflb>();
        for (int i=0;i<whflbs.size();i++){
            String[] st= whflbs.get(i).getGzyy().split("-");
            list.add(st[1]);
        }
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/insertfl", method = RequestMethod.POST)
    public int insertfl(Whflb whflb) {
        whflb.setGzyy(whflb.getGzfl()+"-"+whflb.getGzyy());
        return whflbMapper.insert(whflb);
    }

    @ResponseBody
    @RequestMapping(value = "/getwhflblist",method = RequestMethod.GET)
    public String getwhflblist(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, @RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj,@RequestParam(value = "gsid",required = false,defaultValue = "") String gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        String userid=session.getAttribute("userid").toString();
        List<Whflb> ncList=new ArrayList<>();
        if (userid.equals("1")){
            ncList=whflbMapper.selectList(new QueryWrapper<Whflb>().like("gsid",cxtj).or().like("gzyy",cxtj).or().like("jjfa",cxtj));
        }else {
            System.out.println(userid);
            ncList=whflbMapper.selectList(new QueryWrapper<Whflb>().eq("gsid",gsid));
        }
        PageInfo page=new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public Integer update(Whflb whflb, @PathVariable("id") String id) {
        return whflbMapper.update(whflb, new UpdateWrapper<Whflb>().eq("id", id));
    }

    @ResponseBody
    @RequestMapping(value = "/updatebyid/{ids}", method = RequestMethod.PUT)
    public Integer updatebyid(Whflb whflb, @PathVariable("ids") String ids) {
        return whflbMapper.updatebyid(whflb.getJjfa(),whflb.getGzfl(),ids);
    }

    @PostMapping("/remove/{id}")
    @ResponseBody
    public Integer remove(@PathVariable("id") String id)
    {
        return whflbMapper.delete(new QueryWrapper<Whflb>().eq("id",id));
    }
}